package com.xujl.utilslibrary.system;

import android.app.Application;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xujl on 2017/12/11.
 * application委托类
 */

public class DelegateApplication {

    private Context mApplicationContext;
    private List<RegisterCallback> mRegisterCallbacks;
    private static final DelegateApplication APPLICATION = new DelegateApplication();

    private DelegateApplication () {
        mRegisterCallbacks = new ArrayList<>();
    }

    public static DelegateApplication getInstance () {
        return APPLICATION;
    }

    public static Context getContext () {
        return getInstance().mApplicationContext;
    }

    public void init (Application application) {
        if(mApplicationContext != null){
            throw new IllegalArgumentException("DelegateApplication只能进行一次初始化操作！");
        }
        mApplicationContext = application;
        for (RegisterCallback registerCallback : mRegisterCallbacks) {
            registerCallback.register(application);
        }
    }

    public void addRegisterTask(RegisterCallback registerCallback){
        if(registerCallback != null){
            mRegisterCallbacks.add(registerCallback);
        }
    }


    public interface RegisterCallback {
        void register (Application application);
    }
}
